<?php

namespace App\Http\Controllers\API\Admin;

use App\Models\Cart;
use App\Models\Goods;
use App\Models\GoodsAttribute;
use App\Models\IntegralLog;
use App\Repositories\UsersRepository;
use App\User;
use App\ZL\Controllers\ApiBaseController;
use App\ZL\Library\ErrorCode;
use App\ZL\ORG\Xcx\Common;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

class UsersAPIAdminController extends ApiBaseController
{
    protected $user;
    protected $store;
    protected $order;

    protected $model_name = 'App\User';
    protected $search_field_array = [
        'nickname','in_blacklist','xcx_openid','parent_id','nickname'
    ];
    public function __construct(UsersRepository $usersRepository)
    {
        $this->user = $usersRepository;
    }

    public function authLimit()
    {
        $shop = Auth::guard('admin')->user()->shop;
        $res[] =  ['shop_id', $shop->id];
        return $res;
    }

    public function _after_get($data)
    {
        $data->load('parent');
        $data->load('bonuses');
    }

    public function _after_find(&$data)
    {
        $data->parent;
        $data->bonuses;
    }

    public function beforeStore($id, $request)
    {
        $User = User::find($id);
        $User->destroy();
        return responseZK(0,null,'不能在后台添加用户');
    }

    public function beforeUpdate($id, $request)
    {
        $User = User::find($id);
        if($request['password'])
            $request['password'] = bcrypt($request['password']);
        if($request['integral']!=$User['integral']){
            IntegralLog::create([
                'shop_id'=>$User->shop_id,
                'user_id'=>$User->id,
                'type'=>1,
                'add_type'=>3,
                'junior_id'=>0,
                'admin_id'=>Auth::guard('admin')->id(),
                'title'=>'后台修改菜金',
                'amount'=>$User['integral'],
                'calculate'=>$request['integral'],
            ]);
        }
        $User->fill($request);
        $User->save();
        return responseZK(1,$User);
    }
}
